package com.luomu.base.biz.cookBook.excel.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import com.luomu.base.api.cookBook.excel.bo.AddressInfoImportBO;
import com.luomu.base.api.cookBook.excel.bo.ImportTips;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

public class AddressInfoListener extends AnalysisEventListener<AddressInfoImportBO> {
    private Logger logger = LoggerFactory.getLogger(AddressInfoListener.class);
    /**
     * 自定义用于暂时存储data
     * 可以通过实例获取该值
     */
    private List<AddressInfoImportBO> datas = new ArrayList<>();


    private List<ImportTips> tips = new ArrayList<>();

    private int i = 0;

    @Override
    public void invoke(AddressInfoImportBO data, AnalysisContext context) {
        i++;
        boolean boolerror = false;
        String str = "";

        //姓名
        String consignee = data.getConsignee();
        if (StringUtils.isEmpty(consignee)) {
            boolerror = true;
            str += "[姓名]不能为空 ";
        } else if (consignee.length() > 100) {
            boolerror = true;
            str += "[姓名]字段超过最大100个字符";
        }

        //手机号
        String mobile = data.getMobile();
        String telephone = data.getTelephone();

        //手机号和固定电话不能同时为空
        if (StringUtils.isEmpty(mobile) && StringUtils.isEmpty(telephone)){
            boolerror = true;
            str += "[手机号]和[固定电话]不能同时为空 ";
        }
        if (StringUtils.isNotEmpty(telephone)){
            if (telephone.contains("-")){
                telephone.replace("-","");
            }

            String regex1 = "(\\+\\d+)?(\\d{3,4}\\-?)?\\d{7,8}$";
            if (!Pattern.matches(regex1, telephone)){
                boolerror = true;
                str += "[固定电话]格式有误";
            }
        }
        if (StringUtils.isNotEmpty(mobile)) {
            if (mobile.length() > 20) {
                boolerror = true;
                str += "[手机号]字段超过最大20个字符";
            } else {
                String regex = "(\\+\\d+)?1[3456789]\\d{9}$";
                if (!Pattern.matches(regex, mobile)) {
                    boolerror = true;
                    str += "[手机号]格式有误";
                }
            }
        }

        //所在地区
        String areaAddress = data.getAreaAddress();
        if (StringUtils.isEmpty(areaAddress)) {
            boolerror = true;
            str += "[所在地区]不能为空 ";
        } else if (areaAddress.length() > 100) {
            boolerror = true;
            str += "[所在地区]超过最大100个字符";
        }

        //详细地址
        String detailedAddress = data.getDetailedAddress();
        if (StringUtils.isEmpty(detailedAddress)) {
            boolerror = true;
            str += "[详细地址]不能为空 ";
        } else if (detailedAddress.length() > 200) {
            boolerror = true;
            str += "[详细地址]超过最大200个字符";
        }

        String email = data.getEmail();
        if (StringUtils.isNotEmpty(email)){
            String regex2 = "\\w+@\\w+\\.[a-z]+(\\.[a-z]+)?";
            if (!Pattern.matches(regex2, email)){
                boolerror = true;
                str += "[邮箱]格式有误";
            }
        }

        //地址别名
        String addressAlias = data.getAddressAlias();
        if (StringUtils.isNotEmpty(addressAlias) && addressAlias.length()>7){
            boolerror = true;
            str += "[地址别名]超过最大7个字符";
        }

        //将地址智能识别为省市区
//        if (!boolerror){
//            //京东
//            ParseResult result = AddressParse.parseSingle(areaAddress, 1);
//            //顺丰
//            //SFAddressRspBO sfAddressRspBO = SfSignUtil.disassemblyAddress(recipientAddress);
//            if (result !=null){
//                if(StringUtils.isEmpty(result.getProvince())){
//                    boolerror = true;
//                    str += "[所在地区省名称]不能为空 ";
//                }
//                if(StringUtils.isEmpty(result.getProvince())){
//                    boolerror = true;
//                    str += "[所在地区市名称]不能为空 ";
//                }
//                if(StringUtils.isEmpty(result.getProvince())){
//                    boolerror = true;
//                    str += "[所在地区区/县名称]不能为空 ";
//                }
//                data.setReceiveProvince(result.getProvince());
//                data.setReceiveProvinceCode(result.getProvinceCode()+"");
//                data.setReceiveCity(result.getCity());
//                data.setReceiveCityCode(result.getCityCode()+"");
//                data.setReceiveCounty(result.getArea());
//                data.setReceiveCountyCode(result.getAreaCode()+"");
//                data.setReceiveTowns(result.getTown());
//                data.setReceiveTownsCode(result.getTownCode()+"");
//
//                if (StringUtils.isNotEmpty(result.getDetail())){
//                    data.setDetailedAddress(result.getDetail()+data.getDetailedAddress());
//                }
//            }else {
//                boolerror = true;
//                str += "[所在地区]"+result.getDetail();
//            }
//        }

        if (boolerror) {
            saveTips(context.readRowHolder().getRowIndex() + 1, str, tips);
        } else {
            datas.add(data);
            logger.info("-------------【excel解析结果第context.readRowHolder().getRowIndex() + 1行】-----------{}", JSON.toJSONString(data));
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {

    }

    /**
     * 保存提示信息到集合
     *
     * @param rowIndex 行数
     * @param desc     提示信息
     * @param tips     存入集合
     */
    private void saveTips(Integer rowIndex, String desc, List<ImportTips> tips) {
        ImportTips tip = new ImportTips();
        tip.setRowNum(rowIndex);
        tip.setTips(desc);
        tips.add(tip);
    }

    public List<AddressInfoImportBO> getDatas() {
        return datas;
    }

    public List<ImportTips> getTips() {
        return tips;
    }

    public int getI() {
        return i;
    }
}
